<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <script src="js/vue.js"></script>
    <script src="js/axios.min.js"></script>
    <title>我的优惠券</title>
    <style>
        body {
            margin: 0;
            font-family: 'Helvetica Neue', sans-serif;
            background-color: #fff;
            color: #333;
        }

        .header {
            background-color: #FFBC0D;
            color: white;
            text-align: center;
            padding: 1rem;
            font-size: 1.5rem;
        }

        .coupon-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 1.5rem;
            padding: 2rem;
            list-style: none;
        }

        .coupon-card {
            border: 1px solid #eee;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
            background-color: #fff;
            transition: transform 0.2s ease;
        }

        .coupon-card:hover {
            transform: translateY(-5px);
        }

        .coupon-image {
            width: 100%;
            height: 180px;
            object-fit: cover;
        }

        .coupon-info {
            padding: 1rem;
            text-align: center;
        }

        .coupon-name {
            font-size: 1.1rem;
            margin: 0.5rem 0;
            color: #222;
        }

        .coupon-price {
            color: #FFBC0D;
            font-weight: bold;
        }

        .coupon-time,
        .coupon-status {
            font-size: 0.9rem;
            color: #666;
            margin: 0.3rem 0;
        }

        .status-used {
            color: gray;
        }

        .status-unused {
            color: green;
        }

        .status-expired {
            color: red;
        }
    </style>
</head>
<body>
<header class="header">
    <h1>我的优惠券</h1>
</header>
<div id="app">
    <ul class="coupon-container">
        <li v-for="(coupon, index) in myCouponList" :key="index">
            <div class="coupon-card">
                <img src="images/default.jpg" alt="优惠券图片" class="coupon-image"/>
                <div class="coupon-info">
                    <div class="coupon-name">{{ coupon.couponName }}</div>
                    <div class="coupon-price" v-if="coupon.couponPrice !== undefined">优惠价格: ¥{{ coupon.couponPrice
                        }}
                    </div>
                    <div class="coupon-time">
                        有效期: {{ formatDate(coupon.couponStartTime) }} - {{ formatDate(coupon.couponEndTime) }}
                    </div>
                    <div class="coupon-status" :class="getStatusClass(coupon.status)">
                        状态: {{ formatStatus(coupon.status) }}
                    </div>
                </div>
            </div>
        </li>
    </ul>
</div>

<script>
    new Vue({
        el: "#app",
        data: {
            myCouponList: []
        },
        mounted() {
            this.getMyCoupons();
        },
        methods: {
            getMyCoupons() {
                axios.get("/kfc/mycoupon")
                    .then(res => {
                        const coupons = res.data.data || [];
                        const couponDetailPromises = coupons.map(item => {
                            return axios.get(`/kfc/coupon/${item.couponId}`)
                                .then(detailRes => {
                                    if (detailRes.data && detailRes.data.data) {
                                        item.couponName = detailRes.data.data.couponName || '';
                                        item.couponPrice = detailRes.data.data.couponPrice || 0;
                                    } else {
                                        item.couponName = '';
                                        item.couponPrice = 0;
                                    }
                                    return item;
                                })
                                .catch(() => {
                                    item.couponName = '';
                                    item.couponPrice = 0;
                                    return item;
                                });
                        });
                        return Promise.all(couponDetailPromises);
                    })
                    .then(fullCoupons => {
                        this.myCouponList = fullCoupons;
                    })
                    .catch(error => {
                        console.error("获取我的优惠券失败：", error);
                    });
            },
            formatDate(dateStr) {
                if (!dateStr) return "";
                return dateStr.split('T')[0];
            },
            formatStatus(status) {
                switch (status) {
                    case 0:
                        return "未使用";
                    case 1:
                        return "已使用";
                    case 2:
                        return "已过期";
                    default:
                        return "未知状态";
                }
            },
            getStatusClass(status) {
                switch (status) {
                    case 0:
                        return "status-unused";
                    case 1:
                        return "status-used";
                    case 2:
                        return "status-expired";
                    default:
                        return "";
                }
            }
        }
    });
</script>
</body>
</html>
